home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsargs.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  5.2 KB  |  220 lines

  1. /* Copyright (C) 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsargs.c,v 1.2 2000/09/19 19:00:25 lpd Exp $ */
  20. /* Command line argument list management */
  21. #include "ctype_.h"
  22. #include "stdio_.h"
  23. #include "string_.h"
  24. #include "gsexit.h"
  25. #include "gsmemory.h"
  26. #include "gsargs.h"
  27.  
  28. /* Initialize an arg list. */
  29. void
  30. arg_init(arg_list * pal, const char **argv, int argc,
  31.      FILE * (*arg_fopen) (P2(const char *fname, void *fopen_data)),
  32.      void *fopen_data)
  33. {
  34.     pal->expand_ats = true;
  35.     pal->arg_fopen = arg_fopen;
  36.     pal->fopen_data = fopen_data;
  37.     pal->argp = argv + 1;
  38.     pal->argn = argc - 1;
  39.     pal->depth = 0;
  40. }
  41.  
  42. /* Push a string onto an arg list. */
  43. void
  44. arg_push_memory_string(arg_list * pal, char *str, gs_memory_t * mem)
  45. {
  46.     arg_source *pas;
  47.  
  48.     if (pal->depth == arg_depth_max) {
  49.     lprintf("Too much nesting of @-files.\n");
  50.     gs_exit(1);
  51.     }
  52.     pas = &pal->sources[pal->depth];
  53.     pas->is_file = false;
  54.     pas->u.s.chars = str;
  55.     pas->u.s.memory = mem;
  56.     pas->u.s.str = str;
  57.     pal->depth++;
  58. }
  59.  
  60. /* Clean up an arg list. */
  61. void
  62. arg_finit(arg_list * pal)
  63. {
  64.     while (pal->depth) {
  65.     arg_source *pas = &pal->sources[--(pal->depth)];
  66.  
  67.     if (pas->is_file)
  68.         fclose(pas->u.file);
  69.     else if (pas->u.s.memory)
  70.         gs_free_object(pas->u.s.memory, pas->u.s.chars, "arg_finit");
  71.     }
  72. }
  73.  
  74. /* Get the next arg from a list. */
  75. /* Note that these are not copied to the heap. */
  76. const char *
  77. arg_next(arg_list * pal)
  78. {
  79.     arg_source *pas;
  80.     FILE *f;
  81.     const char *astr = 0;    /* initialized only to pacify gcc */
  82.     char *cstr;
  83.     const char *result;
  84.     int endc;
  85.     int c, i;
  86.     bool in_quote, eol;
  87.  
  88.   top:pas = &pal->sources[pal->depth - 1];
  89.     if (pal->depth == 0) {
  90.     if (pal->argn <= 0)    /* all done */
  91.         return 0;
  92.     pal->argn--;
  93.     result = *(pal->argp++);
  94.     goto at;
  95.     }
  96.     if (pas->is_file)
  97.     f = pas->u.file, endc = EOF;
  98.     else
  99.     astr = pas->u.s.str, f = NULL, endc = 0;
  100.     result = cstr = pal->cstr;
  101. #define cfsgetc() (f == NULL ? (*astr ? *astr++ : 0) : fgetc(f))
  102. #define is_eol(c) (c == '\r' || c == '\n')
  103.     i = 0;
  104.     in_quote = false;
  105.     eol = true;
  106.     c = cfsgetc();
  107.     for (i = 0;;) {
  108.     if (c == endc) {
  109.         if (in_quote) {
  110.         cstr[i] = 0;
  111.         fprintf(stdout, "Unterminated quote in @-file: %s\n", cstr);
  112.         gs_exit(1);
  113.         }
  114.         if (i == 0) {
  115.         /* EOF before any argument characters. */
  116.         if (f != NULL)
  117.             fclose(f);
  118.         else if (pas->u.s.memory)
  119.             gs_free_object(pas->u.s.memory, pas->u.s.chars,
  120.                    "arg_next");
  121.         pal->depth--;
  122.         goto top;
  123.         }
  124.         break;
  125.     }
  126.     /* c != endc */
  127.     if (isspace(c)) {
  128.         if (i == 0) {
  129.         c = cfsgetc();
  130.         continue;
  131.         }
  132.         if (!in_quote)
  133.         break;
  134.     }
  135.     /* c isn't leading or terminating whitespace. */
  136.     if (c == '#' && eol) {
  137.         /* Skip a comment. */
  138.         do {
  139.         c = cfsgetc();
  140.         } while (!(c == endc || is_eol(c)));
  141.         if (c == '\r')
  142.         c = cfsgetc();
  143.         if (c == '\n')
  144.         c = cfsgetc();
  145.         continue;
  146.     }
  147.     if (c == '\\') {
  148.         /* Check for \ followed by newline. */
  149.         c = cfsgetc();
  150.         if (is_eol(c)) {
  151.         if (c == '\r')
  152.             c = cfsgetc();
  153.         if (c == '\n')
  154.             c = cfsgetc();
  155.         eol = true;
  156.         continue;
  157.         }
  158.         /* \ anywhere else is treated as a printing character. */
  159.         /* This is different from the Unix shells. */
  160.         if (i == arg_str_max - 1) {
  161.         cstr[i] = 0;
  162.         fprintf(stdout, "Command too long: %s\n", cstr);
  163.         gs_exit(1);
  164.         }
  165.         cstr[i++] = '\\';
  166.         eol = false;
  167.         continue;
  168.     }
  169.     /* c will become part of the argument */
  170.     if (i == arg_str_max - 1) {
  171.         cstr[i] = 0;
  172.         fprintf(stdout, "Command too long: %s\n", cstr);
  173.         gs_exit(1);
  174.     }
  175.     /* If input is coming from an @-file, allow quotes */
  176.     /* to protect whitespace. */
  177.     if (c == '"' && f != NULL)
  178.         in_quote = !in_quote;
  179.     else
  180.         cstr[i++] = c;
  181.     eol = is_eol(c);
  182.     c = cfsgetc();
  183.     }
  184.     cstr[i] = 0;
  185.     if (f == NULL)
  186.     pas->u.s.str = astr;
  187.   at:if (pal->expand_ats && result[0] == '@') {
  188.     if (pal->depth == arg_depth_max) {
  189.         lprintf("Too much nesting of @-files.\n");
  190.         gs_exit(1);
  191.     }
  192.     result++;        /* skip @ */
  193.     f = (*pal->arg_fopen) (result, pal->fopen_data);
  194.     if (f == NULL) {
  195.         fprintf(stdout, "Unable to open command line file %s\n", result);
  196.         gs_exit(1);
  197.     }
  198.     pal->depth++;
  199.     pas++;
  200.     pas->is_file = true;
  201.     pas->u.file = f;
  202.     goto top;
  203.     }
  204.     return result;
  205. }
  206.  
  207. /* Copy an argument string to the heap. */
  208. char *
  209. arg_copy(const char *str, gs_memory_t * mem)
  210. {
  211.     char *sstr = (char *)gs_alloc_bytes(mem, strlen(str) + 1, "arg_copy");
  212.  
  213.     if (sstr == 0) {
  214.     lprintf("Out of memory!\n");
  215.     gs_exit(1);
  216.     }
  217.     strcpy(sstr, str);
  218.     return sstr;
  219. }
  220.